﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Data;
using DataProvider;

namespace SGBDSecurity
{
    public partial class DeleteRow : Form
    {

        UserDataProvider aUserDataProvider;
        User aUser;
        Table aTable;
        TableData aTableData;

        public DeleteRow(User iUser, Table iTable)
        {
            aTable = iTable;
            aUser = iUser;
            aUserDataProvider = new UserDataProvider();
            InitializeComponent();
            this.lNome.Text = iUser.Login;
            this.lNivel.Text = iUser.Classe + "";
            this.RefreshTable();
            

        }

        public void RefreshTable()
        {
            this.dataGridView1.Rows.Clear();
            aTableData = aUserDataProvider.GetViewTable(aUser, aTable);

            foreach (string temp in aTableData.Colunas)
            {
                DataGridViewColumn column = new DataGridViewColumn();
                column.HeaderText = temp;
                column.CellTemplate = new DataGridViewTextBoxCell();

                this.dataGridView1.Columns.Add(column);
            }
            for (int j = 0; j < aTableData.Valores.Count / aTableData.Colunas.Count; j++)
            {
                bool aPodeMostrar = true;
                if (aTable.STipoSeguranca.Equals("campo"))
                {
                    for (int i = 1; i < this.aTableData.Colunas.Count; i = i + 2)
                    {
                        if (!aTableData.Valores[(j * aTableData.Colunas.Count + i)].Equals(""))
                        {
                            int aTempInt = int.Parse(aTableData.Valores[(j * aTableData.Colunas.Count + i)]);
                            if (aTempInt != aUser.Classe)
                            {
                                aPodeMostrar = false;
                            }
                        }
                    }
                }
                else if (aTable.STipoSeguranca.Equals("registro"))
                {
                    int aTempInt = int.Parse(aTableData.Valores[j * aTableData.Colunas.Count + this.aTableData.Colunas.Count - 1]);
                    if (aTempInt != aUser.Classe)
                    {
                        aPodeMostrar = false;
                    }
                }
                else if (aTable.STipoSeguranca.Equals("tabela"))
                {
                    if (aTable.Classe != aUser.Classe)
                    {
                        aPodeMostrar = false;
                    }
                }
                if (aPodeMostrar)
                {
                    this.dataGridView1.Rows.Add(aTableData.Valores.GetRange(j * aTableData.Colunas.Count, aTableData.Colunas.Count).ToArray());
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                this.lblMessage.Text = "";
                List<string> valores = new List<string>();
                if (aTable.STipoSeguranca.Equals("campo"))
                {
                    valores.Add(aTableData.Colunas[0]);
                    valores.Add(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                    valores.Add(aTableData.Colunas[1]);
                    valores.Add(this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString());
                }
                else if (aTable.STipoSeguranca.Equals("registro"))
                {
                    valores.Add(aTableData.Colunas[0]);
                    valores.Add(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                    valores.Add(aTableData.Colunas[aTableData.Colunas.Count - 1]);
                    valores.Add(this.dataGridView1.SelectedRows[0].Cells[aTableData.Colunas.Count - 1].Value.ToString());
                }
                else
                {
                    valores.Add(aTableData.Colunas[0]);
                    valores.Add(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
                    valores.Add("0");
                    valores.Add("0");
                }

                aUserDataProvider.DeleteTable(aTable, valores);
                PopUp aPop = new PopUp("Row Deleted!");
                aPop.Visible = true;
                this.RefreshTable();
            }
            catch (Exception ex)
            {
                this.lblMessage.Text = ex.Message;
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
